<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['party'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['party'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
  $userTargetArray     = array();
  $totalTarget         = 0;
  $totalAchievedTarget = 0;
  $totalBalanceTarget  = 0;
  $targetCount         = 0;

  $selectTarget = "SELECT usertarget.usertargetId,user.userId,user.userName,DATE_FORMAT(usertarget.month,'%b-%Y')AS month,
                          usertarget.target,usertarget.achievedTarget
                     FROM usertarget
                     JOIN user
                    WHERE usertarget.userId = user.userId";
  if(isset($_GET['filterUserId']))
  {
    if($_GET['filterUserId'] > 0 )
    {
      $selectTarget .= " AND usertarget.userId = ".$_GET['filterUserId'];
    }
  }
  if(isset($_GET['monthFilter'] ) && $_GET['monthFilter'] != '')
    $selectTarget .= " AND usertarget.month = '".$_GET['yearFilter']."-".$_GET['monthFilter']."-01'";
    
  $selectTargetResult = mysql_query($selectTarget);
  while($targetRow = mysql_fetch_array($selectTargetResult))
  {
    $userTargetArray[$targetCount]['usertargetId']   = $targetRow['usertargetId'];
    $userTargetArray[$targetCount]['userName']       = $targetRow['userName'];
    $userTargetArray[$targetCount]['month']          = $targetRow['month'];
    $userTargetArray[$targetCount]['target']         = $targetRow['target'];
    $userTargetArray[$targetCount]['achievedTarget'] = $targetRow['achievedTarget'];
    $userTargetArray[$targetCount]['balanceTarget']  = $targetRow['target'] - $targetRow['achievedTarget'];
    $totalTarget         += $targetRow['target'];
    $totalAchievedTarget += $targetRow['achievedTarget'];
    $totalBalanceTarget  += $targetRow['target'] - $targetRow['achievedTarget'];
    $targetCount++;
  }
  
  include("./bottom.php");
  $smarty->assign("userTargetArray",$userTargetArray);
  $smarty->assign("totalTarget",$totalTarget);
  $smarty->assign("totalAchievedTarget",$totalAchievedTarget);
  $smarty->assign("totalBalanceTarget",$totalBalanceTarget);
  $smarty->display("userNameAj.tpl");
}
?>